<?php

namespace App\Http\Controllers\user;

use App\Dao\BillsDao;
use App\Dao\CommonDao;
use App\Dao\RechargeDao;
use App\Model\SysModel;
use App\Services\Code;
use App\Services\Logger;
use Illuminate\Support\Facades\DB;

class CommonController extends BaseController
{
    public function goods(): array
    {
        return Code::message('',200,CommonDao::goods());
    }

    public function datas()
    {
        return Code::message('',200,CommonDao::datas());
    }

    public function auto_tuo(): array
    {
        $params = array_merge(request()->all(),[
            'user_id'=>getUserToken()
        ]);
        CommonDao::auto_tuo($params);
        return Code::message('',200);
    }

    public function notice_recharge(){
        $postStr = file_get_contents('php://input');
        $getStr = $_GET;
        parse_str($postStr,$postStr);
        Logger::usdtnotice('=motice',[$postStr]);
        $rechargeQuery = RechargeDao::builder_query([
            'order_sn'=>$postStr['order_sn'],
            'status'=>0
        ]);
        if(!$rechargeQuery->exists()){
            echo 'ok';
        }
        $info = $rechargeQuery->first()->toArray();
        DB::beginTransaction();
        try {
            BillsDao::moneyUpdate($info['pay_usdt'],$info['user_id'],'充值到账USDT',1);
            $rechargeQuery->update([
                'status'=>1,
                'pay_time'=>time()
            ]);
            DB::commit();
            echo 'ok';
        }catch (\Exception $exception){
            DB::rollBack();
            Logger::usdtnotice("notice_error",[
                $exception->getMessage()
            ]);
        }
    }


    public function sys()
    {
        return Code::message('',200,SysModel::query()->get());
    }
}
